<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Customer extends Admin_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->loadData('controllerName',$this->router->fetch_class());
	}
	
	public function index(){
		redirect('administrator/customer/manager');
	}
	
	//Quan ly khach hang
	public function manager(){
		$per_page = 20;
		$num_link = 7;
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->loadScript(base_url('js/admin/jquery.modify.js'));
		$this->loadScript(base_url('js/admin/jquery.update.status.js'));
		$this->headTitle('Chương trình phát sóng - Admin Panel');
		
		$currentPage = $this->input->get('page',true);
		$currentPage = (!empty($currentPage))?$currentPage:'1';
		$this->load->model('Customers_Model');
		$countcustomer = $this->Customers_Model->countCustomer();
		$this->loadData('countcustomer',$countcustomer);
		$pagingConfig = $this->init_pagination('administrator/customer/manager?act=paging', $countcustomer, $per_page, $num_link);
		$this->loadData('paging', $this->pagination->create_links());
		$this->loadData('listcustomers', $this->Customers_Model->getCustomersLimit((($currentPage-1) * $pagingConfig['per_page']), $pagingConfig['per_page']));
		if($this->input->post()){
			if($this->form_validation->run('formsearch') == true){
				$keysearch = trim($this->input->post('keysearch', true));
				$fieldsearch = trim($this->input->post('field_search', true));
				$this->loadData('textkeysearch', $keysearch);
				$this->loadData('listcustomers', $this->Customers_Model->searchCustomer($keysearch, $fieldsearch));
			}

		}
		$this->loadView('administrator/customer/manager');
	
	}
	
	//Sua khach hang
	public function modify(){
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('Customers_Model');	
		$cusid = $this->input->get('cusid', true);
		$customer = $this->Customers_Model->getCustomerByCusId($cusid);
		if($customer){
			if($this->input->post()){
				if($this->form_validation->run('customer') == true){	
					$this->Customers_Model->updateCustomer($cusid);
					redirect('administrator/customer/manager');
				}		
			}
			$this->loadData('customer', $customer);
			$this->loadScript(base_url('js/admin/jquery.fixedListcontrol.js'));
			$this->headTitle('Sửa khách hàng - Admin Panel');
			$this->loadView('administrator/customer/modify');
		}else{
			redirect('administrator/customer/manager');
		}
	}
	//Doi trang thai khach hang
	public function change_status(){
		if($this->input->post()){
			$key = $this->input->post('key', true);
			$status = $this->input->post('status', true);
			$this->load->model('Customers_Model');
			$changeStatus = $this->Customers_Model->changeStatusCustomer($key, $status);
			if($changeStatus){
				$json['status'] = $changeStatus;
				$json['success'] = true;
			}else{
				$json['success'] = false;
			}
			echo json_encode($json);
		}
	}
	
	//Xoa khach hang
	public function delete(){
		if($this->input->post()){
			$key = $this->input->post('key', true);
			$this->load->model('Customers_Model');		
			if($this->Customers_Model->deleteCustomer($key)){
				$json["success"] = true;
			}else{
				$json["success"] = false;
			}
			echo json_encode($json);
		}
	}
	
}